﻿using LxBasic;
using LxCore;

namespace LxCore.Samples
{
    public class CavityFlow : SampleVMBase
    {
        public CavityFlow() : base()
        {
            Name = "方腔流动";
            FileName = "Case2-Cavity.msh";
        }
        public override void RunCase()
        {
            ReadFile();
            LxSim.Sim.Info.ProjName = "顶盖驱动流";
            //create physical model
            var physical = LxSim.Sim.Physics.CreatePhysicalModel();
            physical.PhysicalType = PhysicalTypes.流体;
            physical.SolveSetting.MomentumConvScheme = ConvectionSchemes.SecondOrder;
            physical.MaterialProperty.Density = new ConstField.Double(1.2);
            physical.MaterialProperty.Viscosity = new ConstField.Double(1e-5);
            physical.SolveSetting.PressureRelaxFactor = new ConstField.Double(0.3);
            physical.SolveSetting.MomentumRelaxFactor = new ConstField.Double(0.7);
            //3.设置边界条件
            var zone = FindZone("SOLID");
            ResHelper.GetCond(zone).PhysicalModel = physical;

            var thread_UP = FindBound(zone, "UP");
            ResHelper.GetCond(thread_UP).BoundType = BoundTypes.壁面;
            ResHelper.GetCond(thread_UP).IsStaticWall = false;
            ResHelper.GetCond(thread_UP).Velocity = new ConstField.Vector(new Vector3d(1, 0, 0));
            FvBound thread_DOWN = FindBound(zone, "DOWN");
            ResHelper.GetCond(thread_DOWN).BoundType = BoundTypes.壁面;
            FvBound thread_LEFT = FindBound(zone, "LEFT");
            ResHelper.GetCond(thread_LEFT).BoundType = BoundTypes.壁面;
            FvBound thread_RIGHT = FindBound(zone, "RIGHT");
            ResHelper.GetCond(thread_RIGHT).BoundType = BoundTypes.壁面;
            FvBound thread_FRONT = FindBound(zone, "FRONT");
            ResHelper.GetCond(thread_FRONT).BoundType = BoundTypes.对称面;
            FvBound thread_BACK = FindBound(zone, "BACK");
            ResHelper.GetCond(thread_BACK).BoundType = BoundTypes.对称面;
            //3.进行计算参数
            //LxSim.Sim.Solvers.SIMPLEGroup.PISOStepCount = 1;
            MultiThreads.SetThreadsCount(1);
            LxSim.Sim.Steps.IterationStep.MaxStepRule.MaxStep = 400;

            var mon = LxSim.Sim.Monitors.CreateMonitor("max velocity", StatisticTypes.Max);
            mon.ScalarType = ScalarFieldTypes.Velocity;
            mon.SetSelector(zone.Inner, true);

            var graph = LxSim.Sim.Visuals.CreateGraph("流场");
            graph.GraphType = GraphTypes.矢量图;
            graph.ScalarType = ScalarFieldTypes.Velocity;
            graph.SetSelector(zone.Inner, true);
            graph = LxSim.Sim.Visuals.CreateGraph("MassImbalance");
            graph.GraphType = GraphTypes.等值图;
            graph.ScalarType = ScalarFieldTypes.MassImbalance;
            foreach (var t in zone.Bounds)
            {
                graph.SetSelector(t, true);
            }
            graph = LxSim.Sim.Visuals.CreateGraph("压力");
            graph.GraphType = GraphTypes.等值图;
            graph.ScalarType = ScalarFieldTypes.Pressure;
            foreach (var t in zone.Bounds)
            {
                graph.SetSelector(t, true);
            }
            //graph = LxSim.Sim.Visuals.CreateGraph("CellIndex");
            //graph.GraphType = GraphTypes.等值图;
            //graph.ScalarType = ScalarFieldTypes.BoundAdjacentCellIndex;
            //foreach (var t in zone.Bounds)
            //{
            //    graph.SetSelector(t, true);
            //}
            //加载到界面
            RunSimulation();
        }
    }
}
